"""import pymysql
import traceback

USER = 'root'
PASSWORD = '123456'
HOST = '114.215.252.110'
DATABASE = 'jqd'

class Student:
    def connect(self):
        self.db = pymysql.connect(
            user = USER,
            password = PASSWORD,
            host = HOST,
            database = DATABASE,
            cursorclass = pymysql.cursors.DictCursor
        )
        self.cursor = self.db.cursor()
    def close(self):
        self.cursor.close()
        self.db.close()
    def insert_student(self,name,age,address,ssn):
        content_list = []
        self.connect()
        self.name = name
        self.age = age
        self.address = address
        self.ssn = ssn
        str1 = f"insert into person(name,age,address,ssn) values('{name}','{age}','{address}','{ssn}')"
        self.cursor.execute(str1)
        self.db.commit()
        self.close()
if __name__ == '__main__':
    s1 = Student()
    s1.insert_student("zhangsan",19,"shenyang","193813913813")
"""

import pymysql
import traceback
class Student:
    def __init__(self,user,password,host,database):
        self.user = user
        self.password = password
        self.host = host
        self.database = database
    def connect(self):
        self.db = pymysql.connect(user=self.user,password=self.password,host=self.host,database=self.database,cursorclass=pymysql.cursors.DictCursor)
        self.cursor = self.db.cursor()
    def close(self):
        self.cursor.close()
        self.db.close()
    def search(self,type:int,sql):
        self.connect()
        result = None
        if type == 1:
            try:
                self.cursor.execute(sql)
                result = self.cursor.fetchone()
                self.close()
                return result
            except:
                self.db.rollback()
                traceback.print_exc()
                return result
        elif type == 2:
            try:
                self.cursor.execute(sql)
                result = self.cursor.fetchall()
                self.close()
                return result
            except:
                self.db.rollback()
                traceback.print_exc()
                return result
    def __edit(self,sql):
        self.connect()
        try:
            self.cursor.execute(sql)
            self.db.commit()
            self.close()
        except:
            self.db.rollback()
            traceback.print_exc()
    def insert(self,sql):
        self.__edit(sql)
    def delete(self,sql):
        self.__edit(sql)
    def updata(self,sql):
        self.__edit(sql)

if __name__ == '__main__':
    d1 = Student(user='root',password='123456',host='114.215.252.110',database='jqd')

    d1.insert("insert into person (name,age,address,ssn) values('zhangsan','21','shenyang','123112314212')")
    # print(d1.search(1,"select * from person(name,age,address,phone) values ('zhangsan','25','shenyang','2131516161')"))
